package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class StringHandler {

    public static void printlnMessage(String message) {
        System.out.println(message);
    }

    public static void printMessage(String message) {
        System.out.print(message);
    }

    public static String printlnMenuToString(ArrayList<String> menu) {
        String separator = System.getProperty("line.separator");
        String stringExpected = "";

        for (int i = 0; i < menu.size(); i++)
            stringExpected += "[" + (i+1) + "]" + menu.get(i) + separator;

        return stringExpected;
    }

    public static int readUserChoice() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            int userKeyboardInput = Integer.parseInt(br.readLine());
            return userKeyboardInput;
        } catch (Exception e) {
            return 0;
        }
    }

    public static int returnValidInput(int optionListSize) {

        boolean valid;
        int input;
        do {
            input = readUserChoice();
            valid = validateChoiceAndPrintWarning(input, optionListSize);
        } while (!valid);
        return input;
    }

    public static boolean validateChoiceAndPrintWarning(int choice, int optionListSize) {
        if (!(choice <= optionListSize && choice > 0)) {
            StringHandler.printlnMessage("Select a valid option!!");
            return false;
        }
        return true;
    }

    public static void printBook(int index, String title, String author, String availability) {
        System.out.format("%-5d%-80s%-25s%-16s%n", index, title, author, availability);
    }

    public static void printBookHeader() {
        System.out.format("%-5s%-80s%-25s%-16s%n", "Nr.", "Title", "Author", "Availability");
        StringHandler.printlnMessage("-----------------------------------------------------------------------------------------------------------------------------");
    }

    public static void printMenu(ArrayList<String> menu) {
        printlnMessage("\nPlease pick an option:\n");
        printlnMessage(printlnMenuToString(menu));
    }
}
